package com.example.lesson

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.core.BaseViewHolder
import com.example.lesson.entity.Lesson

class LessonAdapter : RecyclerView.Adapter<LessonAdapter.LessonViewHolder>() {
    private var list: List<Lesson> = ArrayList()
    fun updateAndNotify(list: List<Lesson>) {
        this.list = list;
        notifyDataSetChanged();
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LessonViewHolder {
        return LessonViewHolder.onCreate(parent);
    }

    override fun getItemCount(): Int {
        return list.size;
    }

    override fun onBindViewHolder(holder: LessonViewHolder, position: Int) {
        holder.onBind(list[position])
    }

    /**
     * kotlin的静态内部类不用加任何修饰
     * 加internal作用是该类只在本模块中可访问，其它模块隐藏不让访问到
     * 父类BaseViewHolder只有一个带参数的构造器要调用
     */
    class LessonViewHolder internal constructor(itemView: View) : BaseViewHolder(itemView) {
        companion object {
            fun onCreate(parent: ViewGroup): LessonViewHolder {
                return LessonViewHolder(LayoutInflater
                        .from(parent.context)
                        .inflate(R.layout.item_lesson, parent, false));

            }
        }


        fun onBind(lesson: Lesson) {
//            var date = lesson.date;
//            if (date == null) {
//                date = "日期待定";
//            }
//            ?:优化以上代码
            var date = lesson.date?:"日期待定"

            setText(R.id.tv_date, date);
            setText(R.id.tv_content, lesson.content!!);
            /*
              val state = lesson.state
            if (state != null) {
                setText(R.id.tv_state, state.stateName());
                var colorRes = R.color.playback;
                colorRes = when (state) {
                    Lesson.State.PLAYBACK ->   R.color.playback

                    Lesson.State.LIVE ->   R.color.live;

                    Lesson.State.WAIT ->     R.color.wait

                    else -> R.color.playback
                }
                val backgroundColor = itemView.getContext().getColor(colorRes);
                getView<View>(R.id.tv_state).setBackgroundColor(backgroundColor);
            }*/
            lesson.state?.also {
                setText(R.id.tv_state, it.stateName());
                var colorRes = R.color.playback;
                colorRes = when (it) {
                    Lesson.State.PLAYBACK ->   R.color.playback

                    Lesson.State.LIVE ->   R.color.live;

                    Lesson.State.WAIT ->     R.color.wait

                    else -> R.color.playback
                }
                val backgroundColor = itemView.getContext().getColor(colorRes);
                getView<View>(R.id.tv_state).setBackgroundColor(backgroundColor);
            }

        }
    }
}